package be.kul.osgi.ds.myagent.message;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InvalidPropertiesFormatException;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import be.kul.osgi.ds.messageservice.MessageObject;
import be.kul.osgi.ds.myagent.MyAgent;
import be.kul.osgi.ds.xmlstreamservice.XMLStreamService;

public class MessageHandler {

	private MessageObject mo;
	private XMLStreamService service;
	private MyAgent agent;
	public List<BasicBehaviour> bhs;

	
	
	public MessageHandler(MyAgent agent, XMLStreamService xmlservice){
		service = xmlservice;
		this.agent = agent;
		mo = new MessageObject();
		bhs = Collections.synchronizedList(new ArrayList<BasicBehaviour>());
	}
	
	public synchronized void addbehaviour(BasicBehaviour b){
		bhs.add(b);
	}
	
	public synchronized void deletebehaviour(BasicBehaviour b){
		bhs.remove(b);
	}
	
	public synchronized BasicBehaviour getbehaviour(int index){
		return bhs.get(index);
	}
	
	public void HandleMessage(String message, String sender) throws InvalidPropertiesFormatException, IOException
	{
		service.readClass(message, MessageObject.class);
		mo = service.readClass(message, MessageObject.class);
		//mo.setSender(sender);
		
		synchronized (bhs) {
		
			if(mo.getPerformative() == MessageObject.CFP){
				ContractNetResponder cnr = new ContractNetResponder(this.agent, mo.getConversationId());
				bhs.add(cnr);
			}
			
			if(mo.getPerformative() == MessageObject.ASKROLE){
				GiveRole gr = new GiveRole(mo.getConversationId(),this.agent);
				bhs.add(gr);
			}
			
			if(mo.getPerformative() == MessageObject.ASKBUNDLE){
				GiveCapability gr = new GiveCapability(mo.getConversationId(),this.agent);
				bhs.add(gr);
			}
		
			for (int i = 0; i< bhs.size(); i++){
				if (bhs.get(i).isInterested(mo)){
					//System.out.println("@messagehandler - Handlemessage : message is handeled");
					bhs.get(i).HandleMessage(mo);
				}
			}
		}
		
		//System.out.println("Sender is "+ mo.getSender());
		//System.out.println("Messagehandler : Handle Message content of message: " + mo.getContent());
		//System.out.println("Messagehandler : Handle Message performative of message: " + mo.getPerformative());
	}
	
	public synchronized void addci(ContractNetInitiator ci){
		bhs.add(ci);
	}
	
	public synchronized void addAskRole(AskRole ar){
		bhs.add(ar);
	}
	
	public synchronized ContractNetInitiator getci(String ID){
		
		synchronized(bhs){
			Iterator<BasicBehaviour> t = bhs.iterator();
			while (t.hasNext()){
				BasicBehaviour cn = t.next();
				if ((cn instanceof ContractNetInitiator) && (cn.getID()==ID)){
					return (ContractNetInitiator) cn;
				}
			}
		}
		return null;
	}

	public void addcr(){
		
	}
	
	public ContractNetInitiator getcr(){
		
		return null;
	}
	
	public Properties deSerializeFeatures(String xml) throws InvalidPropertiesFormatException, IOException {
		Properties properties = new Properties();
		InputStream is = new ByteArrayInputStream(xml.getBytes());
		properties.loadFromXML(is);
		return properties;
	}
	
	
}
